package com.htu.courseService.websocket.config;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.AllArgsConstructor;

/**
 * Created By JNNarrator on 2025/01/20 16:08
 * email: jiangn09@chinatelecom.cn
 */
@AllArgsConstructor
public class SocketUtil {
	private  ChannelHandlerContext ctx;
	private  Channel channel;
	public static SocketUtil ctx(ChannelHandlerContext ctx){
		return new SocketUtil(ctx);
	}
	public static SocketUtil channel(Channel channel){
		return new SocketUtil(channel);
	}
	public void sendMsg(SocketMsg<?> socketMsg){
		ctx.channel().writeAndFlush(new TextWebSocketFrame(socketMsg.toJSON()));
	}
	public void sendMsgChannel(SocketMsg<?> socketMsg){
		channel.writeAndFlush(new TextWebSocketFrame(socketMsg.toJSON()));
	}
	
	private SocketUtil(ChannelHandlerContext ctx){
		this.ctx = ctx;
	}
	private SocketUtil(Channel channel){
		this.channel = channel;
	}
}
